<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Binh
 * Date: 1/3/14
 * Time: 9:39 PM
 * To change this template use File | Settings | File Templates.
 */
use \Model;
use \Controller_Login;

class Controller_Admin extends Controller{
    //Khai bao trang cau truc admin
    private $view;
    //Khai bao bien toan cuc
    private $img;
    private $author;
    private $genre;
    private $member;
    private $music;
    private $singer;

    public function __construct(){
        //bien toan cuc
        $this->img = Uri::base(false).'assets/img/admin/';
        $this->author = array();
        $this->genre = array();
        $this->member = array();
        $this->music = array();
        $this->singer = array();

        //Khai bao view
        $this->view = View::forge('admin/layout');
        $this->view->head = View::forge('admin/head');
        $this->view->header = View::forge('admin/header');
        $this->view->menu = View::forge('admin/menu');
        $this->view->sidebar_menu = View::forge('admin/sidebar_menu');
        $this->view->table_author = View::forge('admin/table_author');
        $this->view->table_genre = View::forge('admin/table_genre');
        $this->view->table_member = View::forge('admin/table_member');
        $this->view->table_music = View::forge('admin/table_music');
        $this->view->table_singer = View::forge('admin/table_singer');
        $this->view->footer = View::forge('admin/footer');

        //Khai bao bien toan cuc
        $this->view->set_global('img', $this->img);
        $this->set_author($this->author);
        $this->set_genre($this->genre);
        $this->set_member($this->member);
        $this->set_music($this->music);
        $this->set_singer($this->singer);
    }
    public function action_index(){
        return (new Controller_Login())->action_layout_admin();
    }
    //get layout home
    public function action_layout_home(){
        return $this->set_layout_home();
    }

    //get layout author
    public function action_layout_author(){
        return $this->set_layout_author(Model\Author::get_all());
    }
    //get layout genre
    public function action_layout_genre(){
        return $this->set_layout_genre(Model\Genre::get_all());
    }
    //get layout member
    public function action_layout_member(){
        return $this->set_layout_member(Model\Member::get_all());
    }
    //get layout music
    public function action_layout_music(){
        return $this->set_layout_music(Model\Song::get_all());
    }
    //get layout singer
    public function action_layout_singer(){
        return $this->set_layout_singer(Model\Singer::get_all());
    }

    //cài đặt biến toàn cục
    private function set_site($site){
        $this->view->set_global('site',$site);
    }
    private function set_author($data=array()){
        $this->view->set_global('authors', $data);
    }
    private function set_genre($data=array()){
        $this->view->set_global('genres', $data);
    }
    private function set_member($data=array()){
        $this->view->set_global('members', $data);
    }
    private function set_music($data=array()){
        $this->view->set_global('musics', $data);
    }
    private function set_singer($data=array()){
        $this->view->set_global('singers', $data);
    }

    //cài đặt các layout
    private function set_layout_home(){
        $this->set_site('home');
        return $this->view;
    }
    private function set_layout_author($data){
        $this->set_site('author');
        $this->set_author($data);
        return $this->view;
    }
    private function set_layout_genre($data){
        $this->set_site('genre');
        $this->set_genre($data);
        return $this->view;
    }
    private function set_layout_member($data){
        $this->set_site('member');
        $this->set_member($data);
        return $this->view;
    }
    private function set_layout_singer($data){
        $this->set_site('singer');
        $this->set_singer($data);
        return $this->view;
    }
    private function set_layout_music($data){
        $this->set_site('music');
        $this->set_music($data);
        return $this->view;
    }

    //Lấy kết quả search
    public function action_search_author(){
        $chars = $_GET['search_input_author'];
        if(isset($chars) and $chars != "search keyword"){
            return $this->set_layout_author(\Model\Author::search_name($chars));
        }
        else{
            return $this->action_layout_author();
        }
    }
    public function action_search_genre(){
        $chars = $_GET['search_input_genre'];
        if(isset($chars) and $chars != "search keyword"){
            return $this->set_layout_genre(\Model\Genre::search_name($chars));
        }
        else{
            return $this->action_layout_genre();
        }
    }
    public function action_search_member(){
        $chars = $_GET['search_input_member'];
        if(isset($chars) and $chars != "search keyword"){
            return $this->set_layout_member(\Model\Member::search_char($chars));
        }
        else{
            return $this->action_layout_member();
        }
    }
    public function action_search_music(){
        $chars = $_GET['search_input_music'];
        if(isset($chars) and $chars != "search keyword"){
            return $this->set_layout_music(\Model\Song::search_char($chars));
        }
        else{
            return $this->action_layout_music();
        }
    }
    public function action_search_singer(){
        $chars = $_GET['search_input_singer'];
        if(isset($chars) and $chars != "search keyword"){
            return $this->set_layout_singer(\Model\Singer::search_char($chars));
        }
        else{
            return $this->action_layout_singer();
        }
    }

    //Xóa một phần tử trong bảng
    public function action_delete_author($author_id){
        \Model\Author::delete_id($author_id);
        return $this->action_layout_author();
    }
    public function action_delete_genre($genre_id){
        \Model\Genre::delete_id($genre_id);
        return $this->action_layout_genre();
    }
    public function action_delete_member($member_id){
        \Model\Member::delete_id($member_id);
        return $this->action_layout_member();
    }
    public function action_delete_singer($singer_id){
        \Model\Singer::delete_id($singer_id);
        return $this->action_layout_singer();
    }
    public function action_delete_music($music_id){
        \Model\Song::delete_id($music_id);
        return $this->action_layout_music();
    }

    //Xoá danh sách phần tử trong bảng
    public function action_delete_list_author(){
        if(isset($_POST['delete_list_author'])and $_POST['delete_list_author'] == "delete_list"){
            if(isset($_POST['check_author'])){
                $list_author_id = $_POST['check_author'];
                foreach($list_author_id as $author_id){
                    \Model\Author::delete_id($author_id);
                }
            }
            return $this->action_layout_author();
        }
    }
    public function action_delete_list_genre(){
        if(isset($_POST['delete_list_genre'])and $_POST['delete_list_genre'] == "delete_list"){
            if(isset($_POST['check_genre'])){
                $list_genre_id = $_POST['check_genre'];
                foreach($list_genre_id as $genre_id){
                    \Model\Genre::delete_id($genre_id);
                }
            }
            return $this->action_layout_genre();
        }
    }
    public function action_delete_list_member(){
        if(isset($_POST['delete_list_member'])and $_POST['delete_list_member'] == "delete_list"){
            if(isset($_POST['check_member'])){
                $list_member_id = $_POST['check_member'];
                foreach($list_member_id as $member_id){
                    \Model\Member::delete_id($member_id);
                }
            }
            return $this->action_layout_member();
        }
    }
    public function action_delete_list_singer(){
        if(isset($_POST['delete_list_singer'])and $_POST['delete_list_singer'] == "delete_list"){
            if(isset($_POST['check_singer'])){
                $list_singer_id = $_POST['check_singer'];
                foreach($list_singer_id as $singer_id){
                    \Model\Singer::delete_id($singer_id);
                }
            }
            return $this->action_layout_singer();
        }
    }
    public function action_delete_list_music(){
        if(isset($_POST['delete_list_music'])and $_POST['delete_list_music'] == "delete_list"){
            if(isset($_POST['check_music'])){
                $list_music_id = $_POST['check_music'];
                foreach($list_music_id as $music_id){
                    \Model\Song::delete_id($music_id);
                }
            }
            return $this->action_layout_music();
        }
    }

}